package com.base.service;

import com.base.core.model.EmailData;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Map;

/**
 * 邮件服务
 * @author win10
 */
@Service
@Log4j2
public class MailService {

    @Value("${spring.mail.username}")
    private String from;

    private MimeMessage mimeMessage;

    private MimeMessageHelper messageHelper;

    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private TemplateEngine templateEngine;


    /**
     * 发送邮件
     * @param emailData 邮件内容
     * @return this
     * @throws MessagingException
     */
    public MailService sendMail(EmailData emailData) throws MessagingException {
        initEmail(emailData);
        return this;
    }

    /**
     * 添加附件邮件
     * @param fileArr fujian
     * @throws MessagingException
     */
    public MailService addAttachment(String... fileArr) throws MessagingException {
        // 添加附件
        for (String filePath : fileArr) {
            FileSystemResource fileResource = new FileSystemResource(new File(filePath));
            if (fileResource.exists()) {
                String filename = fileResource.getFilename();
                messageHelper.addAttachment(filename, fileResource);
            }
        }
        return this;
    }

    /**
     * 添加图片
     * @param imgMap tupian
     * @return this
     * @throws MessagingException
     */
    public MailService addImage(Map<String, String> imgMap) throws MessagingException {
        for (Map.Entry<String, String> entry : imgMap.entrySet()) {
            FileSystemResource fileResource = new FileSystemResource(new File(entry.getValue()));
            if (fileResource.exists()) {
                String filename = fileResource.getFilename();
                messageHelper.addInline(entry.getKey(), fileResource);
            }
        }
       return this;
    }

    /**
     * 发送模板邮件
     * @param emailData 模板
     * @return this
     */
    public MailService sendTemplate(EmailData emailData, Map<String, Object> data, String templateName) throws MessagingException {
        Context context = new Context();
        context.setVariables(data);
        String template = templateEngine.process(templateName, context);
        emailData.setContent(template);
        sendMail(emailData);
        return this;
    }


    /**
     * 发送
     */
    public void doIt() {
        javaMailSender.send(mimeMessage);
    }

    /**
     *  初始化 主题 内容 发送人 收件人
     * @param emailData 初始化 主题 内容 发送人 收件人
     * @throws MessagingException
     */
    private void initEmail(EmailData emailData) throws MessagingException {
        mimeMessage = javaMailSender.createMimeMessage();
        messageHelper = new MimeMessageHelper(mimeMessage, true);
        messageHelper.setFrom(from);
        messageHelper.setTo(emailData.getTo());
        messageHelper.setSubject(emailData.getSubject());
        messageHelper.setText(emailData.getContent(), true);
    }


}
